<template>
  <el-drawer
    :before-close="handleClose"
    direction="rtl"
    title="主题配置"
    :visible.sync="drawer"
  >
    <el-form label-position="top" size="mini">
      <el-form-item label="主题色">
        <div class="el-row--flex is-align-middle is-justify-space-between">
          <el-input v-model="form.primary" />
          <el-color-picker
            v-model="form.primary"
            class="pl10"
            @change="setColor($event, 'primary')"
          />
        </div>
      </el-form-item>
      <el-form-item label="成功色">
        <div class="el-row--flex is-align-middle is-justify-space-between">
          <el-input v-model="form.success" />
          <el-color-picker
            v-model="form.success"
            class="pl10"
            @change="setColor($event, 'success')"
          />
        </div>
      </el-form-item>
      <el-form-item label="警告色">
        <div class="el-row--flex is-align-middle is-justify-space-between">
          <el-input v-model="form.warning" />
          <el-color-picker
            v-model="form.warning"
            class="pl10"
            @change="setColor($event, 'warning')"
          />
        </div>
      </el-form-item>
      <el-form-item label="危险色">
        <div class="el-row--flex is-align-middle is-justify-space-between">
          <el-input v-model="form.danger" />
          <el-color-picker
            v-model="form.danger"
            class="pl10"
            @change="setColor($event, 'danger')"
          />
        </div>
      </el-form-item>
      <el-form-item label="信息色">
        <div class="el-row--flex is-align-middle is-justify-space-between">
          <el-input v-model="form.info" />
          <el-color-picker
            v-model="form.info"
            class="pl10"
            @change="setColor($event, 'info')"
          />
        </div>
      </el-form-item>
      <el-form-item label="白色">
        <div class="el-row--flex is-align-middle is-justify-space-between">
          <el-input v-model="form.white" />
          <el-color-picker
            v-model="form.white"
            class="pl10"
            @change="setColor($event, 'white')"
          />
        </div>
      </el-form-item>
      <el-form-item label="黑色">
        <div class="el-row--flex is-align-middle is-justify-space-between">
          <el-input v-model="form.black" />
          <el-color-picker
            v-model="form.black"
            class="pl10"
            @change="setColor($event, 'black')"
          />
        </div>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
  </el-drawer>
</template>

<script>
import { changeThemeColor, resetTheme } from "@/utils/themeColorClient";
const config = require("@/config/config");
export default {
  name: "DiyTheme",
  data() {
    return {
      drawer: false,
      form: {
        primary: "",
        success: "",
        warning: "",
        danger: "",
        info: "",
        white: "",
        black: ""
      }
    };
  },
  methods: {
    show() {
      this.drawer = true;
      Object.keys(this.form).forEach(e => {
        this.form[e] = localStorage.getItem(e) || config.theme[e];
      });
    },
    reset() {
      this.form = config.theme;
      resetTheme();
      this.$message.success("换肤成功");
    },
    setColor(e, k) {
      changeThemeColor(e, k).then(() => {
        this.$message.success("换肤成功");
      });
    },
    handleClose(done) {
      done();
    }
  }
};
</script>

<style lang="scss" scoped>
.item {
  height: 40px;
}
::v-deep .el-form-item {
  padding: 0 20px;
}
</style>
